package com.news.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.news.shared.GoogleNews;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class News implements EntryPoint {

	private NewsServiceAsync newsService = GWT.create(NewsService.class);

	private final FlexTable localNewsTable = new FlexTable();
	private final FlexTable nearbyNewsTable = new FlexTable();
	
	private final TextBox searchBox = new TextBox();
	private Button localSearchButton = new Button("Search");
	private Button nearbySearchButton = new Button("Search nearby places");
	
	private int currentLocalRow;
	private int currentNearbyRow;
	
	public void onModuleLoad() {

		searchBox.setText("Minneapolis");
		
		localSearchButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				System.out.println("Local Search Button clicked!");
				
				for (int i = 0; localNewsTable.getRowCount() > 0; ++i) {
					localNewsTable.removeRow(0);
				}

				if(newsService == null){
					newsService = GWT.create(NewsService.class);
				}
				
				//TODO
				newsService.getNews(searchBox.getText(), "date", 7, new AsyncCallback<GoogleNews[]>() {

					@Override
					public void onSuccess(GoogleNews[] result) {
						System.out.println("getNews returned with success");

						for (int i = 0; i < result.length; i++) {
							currentLocalRow++;

							GoogleNews story = result[i]; 
							String imgURL = story.getImageurl();

							localNewsTable.setHTML(currentLocalRow, 0, "<a href=\"" + story.getUrl()
									+ "\">" + story.getTitle() + "</a> <br>" + story.getContent());

							if (imgURL != null){
								localNewsTable.setHTML(currentLocalRow, 1, "<img src=\"" + imgURL + "\">");
							}
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						System.out.println("getNews returned with failure");

					}
				});
				currentLocalRow = 0;
			}

		});
		
		nearbySearchButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				System.out.println("Nearby Search Button Clicked!");
				
				for (int i = 0; nearbyNewsTable.getRowCount() > 0; ++i) {
					nearbyNewsTable.removeRow(0);
				}

				if(newsService == null){
					newsService = GWT.create(NewsService.class);
				}
				
				newsService.getNearbyPlaces(searchBox.getText(), new AsyncCallback<String[]>() {

					@Override
					public void onFailure(Throwable caught) {
						System.out.println("getNearbyPlaces returned with failure");
						
					}

					@Override
					public void onSuccess(String[] result) {
						System.out.println("getNearbyPlaces returned with success");
						//TODO
						newsService.getNews(result[0], "r", 3, new AsyncCallback<GoogleNews[]>() {

							@Override
							public void onSuccess(GoogleNews[] result) {
								System.out.println("getNews, called for nearby news, returned with success");

								for (int i = 0; i < result.length; i++) {
									currentNearbyRow++;

									GoogleNews story = result[i]; 
									String imgURL = story.getImageurl();

									nearbyNewsTable.setHTML(currentNearbyRow, 0, "<a href=\"" + story.getUrl()
											+ "\">" + story.getTitle() + "</a> <br>" + story.getContent());

									if (imgURL != null){
										nearbyNewsTable.setHTML(currentNearbyRow, 1, "<img src=\"" + imgURL + "\">");
									}
								}
							}

							@Override
							public void onFailure(Throwable caught) {
								System.out.println("getNews returned with failure");

							}
						});
						newsService.getNews(result[2], "r", 3, new AsyncCallback<GoogleNews[]>() {

							@Override
							public void onSuccess(GoogleNews[] result) {
								System.out.println("getNews, called for nearby news, returned with success");

								for (int i = 0; i < result.length; i++) {
									currentNearbyRow++;

									GoogleNews story = result[i]; 
									String imgURL = story.getImageurl();

									nearbyNewsTable.setHTML(currentNearbyRow, 0, "<a href=\"" + story.getUrl()
											+ "\">" + story.getTitle() + "</a> <br>" + story.getContent());

									if (imgURL != null){
										nearbyNewsTable.setHTML(currentNearbyRow, 1, "<img src=\"" + imgURL + "\">");
									}
								}
							}

							@Override
							public void onFailure(Throwable caught) {
								System.out.println("getNews returned with failure");

							}
						});
						newsService.getNews(result[1], "r", 3, new AsyncCallback<GoogleNews[]>() {

							@Override
							public void onSuccess(GoogleNews[] result) {
								System.out.println("getNews, called for nearby news, returned with success");

								for (int i = 0; i < result.length; i++) {
									currentNearbyRow++;

									GoogleNews story = result[i]; 
									String imgURL = story.getImageurl();

									nearbyNewsTable.setHTML(currentNearbyRow, 0, "<a href=\"" + story.getUrl()
											+ "\">" + story.getTitle() + "</a> <br>" + story.getContent());

									if (imgURL != null){
										nearbyNewsTable.setHTML(currentNearbyRow, 1, "<img src=\"" + imgURL + "\">");
									}
								}
							}

							@Override
							public void onFailure(Throwable caught) {
								System.out.println("getNews returned with failure");

							}
						});
					}
				});
				currentNearbyRow = 0;
			}
		});

		VerticalPanel vp = new VerticalPanel();
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(searchBox);
		hp.add(localSearchButton);
		hp.add(nearbySearchButton);
		vp.add(hp);
		vp.add(localNewsTable);
		vp.add(nearbyNewsTable);
		RootPanel.get().add(vp);

	}

}
